package com.uchedao.seller.ui.car.publish;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonObject;
import com.uchedao.seller.R;
import com.uchedao.seller.config.Api;
import com.uchedao.seller.config.GlobalInfo;
import com.uchedao.seller.http.HttpRequest;
import com.uchedao.seller.inf.ICheckValueListener;
import com.uchedao.seller.manager.ActivityManager;
import com.uchedao.seller.ui.car.base.BasePublishActivity;
import com.uchedao.seller.ui.car.manage.CarForSaleActivity;
import com.uchedao.seller.util.Arith;
import com.uchedao.seller.util.CheckValueUtil;
import com.uchedao.seller.util.ProgressDialogUtil;
import com.uchedao.seller.util.T;
import com.uchedao.seller.util.Utility;
import com.uchedao.seller.util.ViewUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 发布5-竞拍规则
 */
public class PublishBidRuleActivity extends BasePublishActivity {

    private EditText edPrice;

    private int carId;
    private double price;
    private boolean publish; // 新发布卖车
    private int aucMinute;

    @Override
    protected String getActivityTag() {
        return "PublishBidRuleActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_publish_bid_rule;
    }

    @Override
    protected void initView() {
        initToBar("设定竞价规则");
        initBottomBar();
        btnTopback.setVisibility(View.GONE);
        btnBottomNext.setText("现在就开始全国竞价");
        btnButtomSave.setText("保存为待售车辆，稍后竞价");

        carId = getIntent().getIntExtra("carId", 0);
        price = getIntent().getDoubleExtra("price", 0);
        publish = getIntent().getBooleanExtra("publish", false);

        if(!publish)
            btnButtomSave.setVisibility(View.GONE);

        aucMinute = 30;

        edPrice = (EditText) findViewById(R.id.edBidRulePrice);

        // 出价时长
        CheckValueUtil.textBtnListener((LinearLayout) findViewById(R.id.llPublishBidRuleRoot),
                R.id.tvBidRuleDuration, R.id.btnBidRuleDuration,
                R.array.bid_duration_strValues, "30分钟", new ICheckValueListener() {
                    @Override
                    public void getValue(View v, String str) {
                        aucMinute = Integer.valueOf(str.substring(0, 2));
                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        btnBottomNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 设定竞拍规则提交发拍
                Utility.hideSoftInput(PublishBidRuleActivity.this);
                submitAuctionRule();
            }
        });
        btnButtomSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 保存待售，退出
                startActivity(new Intent(PublishBidRuleActivity.this, CarForSaleActivity.class));
                ActivityManager.getInstance().clearAllActivity(null);
            }
        });
    }

    /**
     * 设定竞拍规则
     */
    public void submitAuctionRule() {
        if(ViewUtil.checkEditEmpty(edPrice, "请填写售价"))
            return;
        if(price>0 && (Double.valueOf(edPrice.getText().toString()) >= (Arith.mul(Arith.div(price, 10000), 0.995)))) {
            ViewUtil.showEditError(edPrice, "降价幅度不足千分之五，需要重新填写");
            return;
        }

        ProgressDialogUtil.showProgressDlg(this, "提交竞价");
        Map<String, String> map = new HashMap<String, String>();
        map.put("app_name", GlobalInfo.APP_NAME);
        map.put("sale_id", carId+"");
        map.put("price", Arith.mul(Double.valueOf(edPrice.getText().toString()),10000)+"");
        map.put("auc_minute", aucMinute+"");
        Request req = HttpRequest.getRequest(HttpRequest.POST, Api.Action.AUCTION_RULE, map,
                new Response.Listener<JsonObject>() {
                    @Override
                    public void onResponse(JsonObject response) {
                        ProgressDialogUtil.dismissProgressDlg();
                        startActivity(new Intent(PublishBidRuleActivity.this, PublishInviteActivity.class));
                        finish();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        ProgressDialogUtil.dismissProgressDlg();
                        T.showShort(PublishBidRuleActivity.this, error.getMessage());
                    }
                });
        addQueue(req);
    }

    @Override
    public void onBackPressed() {
        if(!publish)
            super.onBackPressed();
    }
}
